#include "SCalendarWidget.h"

#include "TimeHelper.h"
#include "Widgets/Input/SButton.h"
#include "Widgets/Layout/SConstraintCanvas.h"

#include "Widgets/Layout/SSpacer.h"
#include "Widgets/Colors/SColorBlock.h"
#include "Layout/Visibility.h"
#include "ListLibrary/GlobalConfig.h"
#include "ListLibrary/ListUIStyle.h"
#include "ListLibrary/BaseWidget/STextBlock_TextStyleLambda_List.h"
#include "Widgets/Text/STextBlock.h"


void SCalendarWidget::Construct(const FArguments& InArgs)
{
	DateTime = InArgs._InDateTime;
	OnUpdateData = InArgs._OnUpdateData;
	AstrictDate = InArgs._InAstrictDate;
	DisMode = InArgs._InDisMode;
	if (DateTime)
	{
		InitButtons();
		FString WeekChar = TEXT("一二三四五六日");
				if(*DateTime == -1)
		{
			Year = 1970;
			Month = 1;
		}
		else
		{
			Year = GanttTimeHelper::GetYear(*DateTime);
			Month = GanttTimeHelper::GetMonth(*DateTime);
		}

#pragma region 日控件
		TSharedRef<SConstraintCanvas> dayWidget = SNew(SConstraintCanvas);
		for (int i = 0; i < WEEK_DAY; i++)
		{
			dayWidget->AddSlot()
			         .Offset(FMargin(i * 36, 0, 24, 20))
			         .Alignment(FVector2D(0.0f, 0.0f))
			[
				SNew(STextBlock_TextStyleLambda_List)
				.TEXTSTYLE("AlibabaBold_11_FFFFFFD9")
				.Justification(ETextJustify::Center) //对齐方式
				.Text(FText::FromString(WeekChar.Mid(i, 1)))
			];
		}
		for (int i = 0; i < MONTH_WEEK; i++)
		{
			for (int j = 0; j < WEEK_DAY; j++)
			{
				dayWidget->AddSlot().Offset(FMargin(j * 36, (i + 1) * 26, 24, 24))
				         .Alignment(FVector2D(0.0f, 0.0f))
				[
					BtnDay[i][j].ToSharedRef()
				];
			}
		}
		SAssignNew(TodayText, STextBlock_TextStyleLambda_List)
		.Text(FText::FromString(TEXT("今天")))
		.TEXTSTYLE("AlibabaRegular_11_FFFFFFA6")
		.Justification(ETextJustify::Center); //对齐方式;
#pragma endregion

#pragma region 合并
		ChildSlot
		[
			SNew(SOverlay)
			+ SOverlay::Slot()
			[
				SNew(SColorBlock).Color(FLinearColor(0.015,0.033,0.078,1))
			]

			+ SOverlay::Slot().Padding(FMargin(5))
			[

				SNew(SVerticalBox)
				+ SVerticalBox::Slot()
				.AutoHeight()
				[
					SNew(SBox).Padding(FMargin(12))
					[
						SNew(SHorizontalBox)
						+ SHorizontalBox::Slot()
						  .AutoWidth()
						  .VAlign(EVerticalAlignment::VAlign_Center)
						[
							SNew(SButton)
							.ButtonStyle(ListUIStyle::GetButtonStyle("transparencyStyle"))
							.OnHovered_Lambda([this]()
										 {
											 SubYear->SetColorAndOpacity(FLinearColor(0.0185, 0.3, 0.822, 1));
										 })
							.OnUnhovered_Lambda([this]()
										 {
											 SubYear->SetColorAndOpacity(FLinearColor(1, 1, 1, 0.85));
										 })
							.OnClicked_Lambda([this]()
							             {
								             Year--;
								             if (Year < 1980)
									             Year = 1980;
								             UpdataButtonsState();
								             return FReply::Handled();
							             })
							[
								SAssignNew(SubYear, STextBlock_TextStyleLambda_List)

							.TEXTSTYLE("AlibabaBold_12_FFFFFFD9")
							.Text(FText::FromString("<<"))
							]

						]
						+ SHorizontalBox::Slot().AutoWidth()
						                        .VAlign(EVerticalAlignment::VAlign_Center)
						[
							SNew(SButton)
							.ButtonStyle(ListUIStyle::GetButtonStyle("transparencyStyle"))
							.OnHovered_Lambda([this]()
										 {
											 SubMonth->SetColorAndOpacity(FLinearColor(0.0185, 0.3, 0.822, 1));
										 })
							.OnUnhovered_Lambda([this]()
										 {
											 SubMonth->SetColorAndOpacity(FLinearColor(1, 1, 1, 0.85));
										 })
							.OnClicked_Lambda([this]()
							             {
								             Month--;
								             if (Month == 0)
									             Month = 12;
								             UpdataButtonsState();
								             return FReply::Handled();
							             })
							[
								SAssignNew(SubMonth, STextBlock_TextStyleLambda_List)

								.TEXTSTYLE("AlibabaBold_12_FFFFFFD9")
								.Text(FText::FromString("<"))
							]
						]
						+ SHorizontalBox::Slot().AutoWidth()
						                        .Padding(FMargin(35, 0, 35, 0))
						                        .HAlign(EHorizontalAlignment::HAlign_Center)
						                        .VAlign(EVerticalAlignment::VAlign_Center)
						[
							SNew(STextBlock_TextStyleLambda_List)
							.TEXTSTYLE("AlibabaBold_12_FFFFFFD9")
							.Text_Lambda([this]() -> FText
							{
								FString Data = FString::Format(TEXT("{0}年 {1}月"), {Year, Month});
								return FText::FromString(Data);
							})
						]
						+ SHorizontalBox::Slot()
						  .AutoWidth()
						  .VAlign(EVerticalAlignment::VAlign_Center)
						[
							SNew(SButton)
							.ButtonStyle(ListUIStyle::GetButtonStyle("transparencyStyle"))
							.OnClicked_Lambda([this]()
							             {
								             Month++;
								             if (Month > 12)
									             Month = 1;
								             UpdataButtonsState();
								             return FReply::Handled();
							             })
							             .OnHovered_Lambda([this]()
													  {
														  AddMonth->SetColorAndOpacity(FLinearColor(0.0185, 0.3, 0.822, 1));
													  })
										 .OnUnhovered_Lambda([this]()
													  {
														  AddMonth->SetColorAndOpacity(FLinearColor(1, 1, 1, 0.85));
													  })
							[
								SAssignNew(AddMonth, STextBlock_TextStyleLambda_List)
								.TEXTSTYLE("AlibabaBold_12_FFFFFFD9")
								.Text(FText::FromString(">"))
							]
						]
						+ SHorizontalBox::Slot()
						  .AutoWidth()
						  .VAlign(EVerticalAlignment::VAlign_Center)
						[
							SNew(SButton)
							.ButtonStyle(ListUIStyle::GetButtonStyle("transparencyStyle"))
							.OnHovered_Lambda([this]()
							             {
								             AddYear->SetColorAndOpacity(FLinearColor(0.0185, 0.3, 0.822, 1));
							             })
							.OnUnhovered_Lambda([this]()
							             {
								             AddYear->SetColorAndOpacity(FLinearColor(1, 1, 1, 0.85));
							             })
							.OnClicked_Lambda([this]()
							             {
								             Year++;
								             UpdataButtonsState();
								             return FReply::Handled();
							             })
							[
								SAssignNew(AddYear, STextBlock_TextStyleLambda_List)

							.TEXTSTYLE("AlibabaBold_12_FFFFFFD9")
							.Text(FText::FromString(">>"))
							]
						]
					]
				]
				+ SVerticalBox::Slot().AutoHeight()
				[
					SNew(SBox).HeightOverride(1)
					[
						SNew(SColorBlock).Color(FLinearColor(1, 1, 1, 0.14))
					]
				]
				+ SVerticalBox::Slot()
				.AutoHeight()
				[
					SNew(SBox).Padding(FMargin(16))
					[
						dayWidget
					]
				]
				+ SVerticalBox::Slot().AutoHeight()
				[
					SNew(SBox).HeightOverride(1)
					[
						SNew(SColorBlock).Color(FLinearColor(1, 1, 1, 0.14))
					]
				]
				+ SVerticalBox::Slot().AutoHeight()
				[
					SNew(SBox).HeightOverride(39)
					[
						SNew(SHorizontalBox)
						+ SHorizontalBox::Slot()
						[
							SNew(SSpacer)
						]
						+ SHorizontalBox::Slot()
						[
							SNew(SButton)
							.ButtonStyle(ListUIStyle::GetButtonStyle("transparencyStyle"))
							.VAlign(VAlign_Center)
							.OnHovered_Lambda([this]()
							             {
								             TodayText->SetColorAndOpacity(FLinearColor(0, 0.47, 0.99, 1));
							             })
							.OnUnhovered_Lambda([this]()
							             {
								             TodayText->SetColorAndOpacity(FLinearColor(1, 1, 1, 0.85));
							             })
							 .OnClicked_Lambda([this]()
							             {
								             //获取今天的时间
								             *DateTime = GanttTimeHelper::GetTodayZero();
								             Year = GanttTimeHelper::GetYear(*DateTime);
								             Month = GanttTimeHelper::GetMonth(*DateTime);
								             if (OnUpdateData.IsBound())
								             {
									             OnUpdateData.Execute(Year, Month, GanttTimeHelper::GetDay(*DateTime));
								             }

								             FSlateApplication::Get().DismissAllMenus(); //关闭日历菜单
								             return FReply::Handled();
							             })
							[
								TodayText.ToSharedRef()
							]
						]
						+ SHorizontalBox::Slot()
						[
							SNew(SSpacer)
						]
					]
				]
			]
		];

#pragma endregion
	}
	UpdataButtonsState();
}

FReply SCalendarWidget::ButtonClick()
{
	for (int i = 0; i < MONTH_WEEK; i++)
	{
		for (int j = 0; j < WEEK_DAY; j++)
		{
			if (BtnDay[i][j]->IsHovered()) //鼠标在那个按钮上盘旋也就是在那个按钮上按下的按钮
			{
				*DateTime = GanttTimeHelper::DateToTimestamp(Year, Month, FCString::Atoi(*TextBlocks[i][j]->GetText().ToString()));
				if (OnUpdateData.IsBound())
				{
					OnUpdateData.Execute(Year, Month, FCString::Atoi(*TextBlocks[i][j]->GetText().ToString()));
				}
				break;
			}
		}
	}
	FSlateApplication::Get().DismissAllMenus(); //关闭日历菜单
	return FReply::Handled();
}

//初始化所有的按钮
void SCalendarWidget::InitButtons()
{
	for (int i = 0; i < MONTH_WEEK; i++)
	{
		for (int j = 0; j < WEEK_DAY; j++)
		{
			//设置字体
			TextBlocks[i][j] = SNew(STextBlock_TextStyleLambda_List)
			.Text(FText::FromString(TEXT("1")))
			.TEXTSTYLE("AlibabaRegular_11_FFFFFFA6")
			.Justification(ETextJustify::Center); //对齐方式;
			//设置按钮
			BtnDay[i][j] = SNew(SButton)
			.ButtonStyle(ListUIStyle::GetButtonStyle("CalendarStyle"))
			.HAlign(HAlign_Center)
			.OnPressed_Lambda([i,j,this]()
			                            {
				                            TextBlocks[i][j]->SetColorAndOpacity(FLinearColor(1, 1, 1, 1));
			                            })
			.OnHovered_Lambda([i,j,this]()
			                            {
				                            TextBlocks[i][j]->SetColorAndOpacity(FLinearColor(0, 0.45, 1, 1));
			                            })
			.OnUnhovered_Lambda([i,j,this]()
			                            {
				                            TextBlocks[i][j]->SetColorAndOpacity(FLinearColor(1.0f, 1.0f, 1.0f, 0.85));
			                            })
			.OnClicked(this, &SCalendarWidget::ButtonClick)
			[
				TextBlocks[i][j].ToSharedRef()
			];
		}
	}
}

void SCalendarWidget::UpdataButtonsState()
{
	uint16 MonthFirstDayWeek = GanttTimeHelper::GetFirstDayWeekForMonth(Year, Month); //本月第一天是星期几
	uint16 MonthDays = GanttTimeHelper::GetMonthDays(Year, Month); //本月共有多少天
	int index = 0;

	//先将所有的按钮隐藏
	for (index = 0; index < MONTH_WEEK * WEEK_DAY; index++)
	{
		BtnDay[index / WEEK_DAY][index % WEEK_DAY]->SetVisibility(EVisibility::Hidden);
		TextBlocks[index / WEEK_DAY][index % WEEK_DAY]->SetColorAndOpacity(FLinearColor(1.0f, 1.0f, 1.0f, 0.85));
	}

	for (int i = 0; i < MonthDays; i++) //再将该月对应的日期显示出来
	{
		int item = i + (MonthFirstDayWeek - 1);
		BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetVisibility(EVisibility::Visible);
		TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetText(FText::FromString(FString::FromInt(i + 1)));
		//if (AstrictDate&&(DisMode == DisabledMode::BEFORE && GanttTimeHelper::DateToTimestamp(Year, Month, i + 1) < GanttTimeHelper::Get00_00_00ToTimestamp((*AstrictDate)) ||
		//	DisMode == DisabledMode::LAFTER && GanttTimeHelper::DateToTimestamp(Year, Month, i + 1) > GanttTimeHelper::Get00_00_00ToTimestamp((*AstrictDate))))
		//{//超过该时间段则不给予点击
		//	TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(false);
		//	BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(false);
		//}
		//else if (GanttTimeHelper::DateToTimestamp(Year, Month, i + 1) == GanttTimeHelper::Get00_00_00ToTimestamp((*DateTime)))
		//{
		//	TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
		//	BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
		//	TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetColorAndOpacity(FLinearColor(1.0f, 0.0f, 0.0f));
		//}
		//else
		{
			TextBlocks[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
			BtnDay[item / WEEK_DAY][item % WEEK_DAY]->SetEnabled(true);
		}
	}
}
